1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*!
Utilities and fragments for implementing the main parser
*/

use super::*;

/// Parse whitespace between `isotope` identifiers
///
/// This includes both actual whitespace and comments
///
/// # Example
/// ```rust
/// # use isotope::parser::ws;
/// assert_eq!(
///     ws("  /* multiline // comment */ // line comment \ndone").unwrap().0,
///     "done"
/// );
/// ```
pub fn ws(input: &str) -> IResult<&str, &str> {
    recognize(many1_count(alt((
        multispace1,
        line_comment,
        multiline_comment,
    ))))(input)
}

/// Parse a single line comment
///
/// Single-line comments begin with `//` and run to the end of the line, e.g.
/// ```text
/// // I'm a single line comment
/// ```
///
/// # Example
/// ```rust
/// # use isotope::parser::line_comment;
/// assert_eq!(
///     line_comment("//line comment \n//hello"),
///     Ok(("\n//hello", "line comment "))
/// );
/// assert_eq!(
///     line_comment("// // nested\nhello"),
///     Ok(("\nhello", " // nested"))
/// )
/// ```
pub fn line_comment(input: &str) -> IResult<&str, &str> {
    preceded(tag("//"), not_line_ending)(input)
}

/// Parse a multi-line comment.
///
/// Multi-line comments are delimited by `/*` and `*/`, and may be nested, e.g.
/// ```text
/// /*
/// I'm a comment!
///     /*
///     I'm a nested comment!
///     */
/// */
/// ```
///
/// # Example
/// ```rust
/// # use isotope::parser::multiline_comment;
/// assert_eq!(
///     multiline_comment("/* comment */hello"),
///     Ok(("hello", " comment "))
/// );
/// assert_eq!(
///     multiline_comment("/*/*nested*/*//*other*/"),
///     Ok(("/*other*/", "/*nested*/"))
/// );
/// assert_eq!(
///     multiline_comment(
///         "/* deeply /*/* nested */ and */ se /* parated */ */"
///     ),
///     Ok(("", " deeply /*/* nested */ and */ se /* parated */ "))
/// );
/// ```
pub fn multiline_comment(input: &str) -> IResult<&str, &str> {
    fn multiline_inner(input: &str) -> IResult<&str, ()> {
        let mut chars = input.chars();
        let mut rest = input;
        while let Some(c) = chars.next() {
            if c == '*' {
                if let Some('/') = chars.next() {
                    break;
                }
            }
            if c == '/' {
                if let Some('*') = chars.next() {
                    let (after_comment, _) = multiline_comment(rest)?;
                    chars = after_comment.chars();
                }
            }
            rest = chars.as_str();
        }
        Ok((rest, ()))
    }
    delimited(tag("/*"), recognize(multiline_inner), tag("*/"))(input)
}

/// Parse a decimal integer literal as a u64.
///
/// # Examples
/// ```rust
/// # use isotope::parser::u64_dec;
/// assert_eq!(u64_dec("5"), Ok(("", 5)));
/// // Does not recognize hexadecimal literals, etc!
/// assert_eq!(u64_dec("0x5"), Ok(("x5", 0)));
/// // Overflow!
/// assert!(u64_dec("100000000000000000000").is_err());
/// ```
pub fn u64_dec(input: &str) -> IResult<&str, u64> {
    map_res(digit1, str::parse)(input)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn line_comment_works() {
        let comment_examples = [
            "// I'm a line comment",
            "// I'm a line comment, with /* a non-nested comment */",
            "// /* I'm a // line comment with a /* nested */ multiline (and line) comment */",
        ];
        for s in comment_examples.iter().copied() {
            assert_eq!(line_comment(s).unwrap().0, "")
        }
    }

    #[test]
    fn multiline_comment_works() {
        let comment_examples = [
            "/* Hello, I'm a /* nested /* comment */ */ */",
            "/* I'm a non-nested comment */",
            "/* I'm a // line comment in a /* nested */ multiline comment */",
        ];
        for s in comment_examples.iter().copied() {
            assert_eq!(multiline_comment(s).unwrap().0, "")
        }
    }

    #[test]
    fn u64_dec_works() {
        let full_examples = [("53", 53), ("456", 456), ("352", 352)];
        for (s, n) in full_examples.iter().copied() {
            assert_eq!(u64_dec(s), Ok(("", n)))
        }
    }
}